﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace DotNET4Study.同步学习
{
    /// <summary>
    /// threadA.Join()方法是用来阻塞当前(可能是主线程的)线程，
    /// 直到在threadA结束执行之后才开始执行当前(可能是主线程的)线程的下一步。
    /// 即阻塞调用方的执行，而开始被调用方threadA的执行。
    /// （其实Join的意思就是把被调用方“加入”到调用方"队列"中.）
    /// </summary>
    class JoinStudy
    {
        public static void Execute()
        {
            Thread.CurrentThread.Name = "MainThread";

            Thread newThread = new Thread(new ThreadStart(ThreadFuncOne));
            newThread.Name = "NewThread";

            for (int j = 0; j < 50; j++)
            {
                if (j == 10)
                {
                    newThread.Start();
                    newThread.Join();
                }
                else
                {
                    Console.WriteLine(Thread.CurrentThread.Name + "   j =  " + j);
                }
            }
            Console.Read();
        }

        private static void ThreadFuncOne()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name + "   i =  " + i);
            }
            Console.WriteLine(Thread.CurrentThread.Name + " has finished");
        }
    }
}
